﻿using System;
using System.Collections.Generic;

namespace CampusSite.Support.Extensions
{
    public static class StringExtensions
    {
        public static string NotCompletedStringIfEmpty(this string text)
        {
            return (string.IsNullOrEmpty(text)) ? "-" : text;
        }
       
        public static int AsInt(this string text)
        {
            int value;
            int.TryParse(text, out value);
            return value;
        }

        public static bool AsBool(this string text)
        {
            bool value;
            bool.TryParse(text, out value);
            return value;
        }

        public static decimal AsDecimal(this string text)
        {
            decimal value;
            decimal.TryParse(text, out value);
            return value;
        }

        public static double AsDouble(this string text)
        {
            double value;
            double.TryParse(text, out value);
            return value;
        }

        public static T AsEnum<T>(this string text)
        {
            return (T)Enum.Parse(typeof(T), text);
        }

        public static T AsEnumOrDefault<T>(this string text, T @default)
        {
            try { return text.AsEnum<T>(); }
            catch { return @default; }
        }

        public static string From(this string text, string start)
        {
            var index = text.IndexOf(start);
            return index == -1 ? string.Empty : text.Substring(index + start.Length);
        }
        public static string Until(this string text, params string[] ends)
        {
            var value = text;
            foreach (var end in ends)
            {
                var index = text.IndexOf(end);
                if (index <= -1) continue;

                var tryValue = text.Substring(0, index);
                if (tryValue.Length < value.Length)
                    value = tryValue;
            }
            return value;
        }
        public static string Remove(this string text, string textToRemove)
        {
            return text.Replace(textToRemove, string.Empty);
        }

        public static string FormatWith(this string format, params object[] args)
        {
            return string.Format(format, args);
        }

        public static bool IsNullOrEmpty(this string stringToTest)
        {
            return string.IsNullOrEmpty(stringToTest);
        }
        private static readonly string[] WordSpeparators = new[] { ",", ":", ";", ".", "$", "(", ")", " ", "#", Environment.NewLine, "\"" };

        public static IEnumerable<string> Words(this string text)
        {
            return text.Split(WordSpeparators, StringSplitOptions.RemoveEmptyEntries);
        }

        public static IEnumerable<string> Words(this string text, string[] wordSeparators)
        {
            return text.Split(wordSeparators, StringSplitOptions.RemoveEmptyEntries); 
        }

        public static string Concatenate(this string text, string moreText)
        {
            return string.Concat(text, moreText);
        }


        public static byte[] GetBytesFromStringSeparetedByGuion(string str)
        {            
            string[] tokens = str.Split('-'); // asumo que los bytes esta     separados por -
            byte[] bytes = new byte[tokens.Length];

            for(int i = 0; i < tokens.Length; i++) {
                bytes[i] = Convert.ToByte(tokens[i]);
            }

        return bytes;

        }

        public static string GetStringSeparatedByGuion(byte[] bytes)
        {
            string str =string.Empty;
            for (int x = 0; x < bytes.Length; x++)
            {
                if(x + 1 == bytes.Length)
                    //para no escribir el ultimo guion
                    str += bytes[x].ToString();
                else
                    str += string.Concat(bytes[x].ToString(), "-");
            }
            return str;
        }
    }
}
